<?php
/**
 * 产品的hash类属性，便于搜索
 *
 * @author xyma
 *
 */
class NodeHash
{
    public static function get($node_id, $hash_id)
    {
        return Db::get(__CLASS__)->select()->where(array('node_id'=>$node_id, 'hash_id'=>$hash_id))->getOne();
    }

    public static function getList($where=null)
    {
        return Db::get(__CLASS__)->select()->where($where)->order('node_id,hash_id')->getAll();
    }
    public static function count($where=null)
    {
        return Db::get(__CLASS__)->count()->where($where)->getCol();
    }

    public static function delete($node_id, $hash_id)
    {
    	return Db::get(__CLASS__)->delete()->where(array('node_id'=>$node_id, 'hash_id'=>$hash_id))->exec();
    }
    public static function deleteByNode($node_id)
    {
    	return Db::get(__CLASS__)->delete()->where(array('node_id'=>$node_id))->exec();
    }
	
    public static function store($node_id, $hash_id, $hash_path=null)
    {
		if(is_array($hash_id))
		{
			foreach($hash_id as $hid){
				self::store($node_id, $hid);
			}
			return true;
		}
    	if($hash_path===null){
    		$ppath	=	Db::get('Cate')->select('path')->where(array('hid'=>$hash_id))->getCol();
    		if(false===$ppath) return false;
    		$hash_path	=	$ppath ? "{$ppath}{$hash_id}/" : "/{$hash_id}/";
    	}
    	return Db::get(__CLASS__)->replace(array(
    		'node_id'	=>	$node_id,
    		'hash_id'	=>	$hash_id,
    		'hash_path'	=>	$hash_path,
    	))->exec();
    }
    
}
